package com.brp2p.controller.mobile;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.brp2p.persistence.entity.Loan;
import com.brp2p.persistence.entity.LoanReceipt;
import com.brp2p.persistence.entity.LoanSearchCondition;
import com.brp2p.service.LoanReceiptService;
import com.brp2p.service.LoanService;
import com.brp2p.service.NotifyService;
import com.brp2p.service.enums.Notify;
import com.brp2p.service.enums.Receipt;
import com.brp2p.utils.AjaxMsg;
import com.brp2p.utils.BaseSupport;
import com.brp2p.utils.CheckCodeUtil;

@Controller
@RequestMapping("mobile/loans")
public class MobileLoanController extends BaseSupport {
	/**
	 * 投资列表每次请求的数量 
	 **/
	private static final int PAGE_SIZE = 5;
	@Autowired
	LoanService loanService;
	@Autowired
	private LoanReceiptService loanReceiptService;
	@Autowired
	private NotifyService notifyService;
	/**
	 * 获取投资列表信息
	 * @author Ranxc
	 * @param loanSearchCondition
	 * @return
	 */
	@RequestMapping(value="getLoans")
	public @ResponseBody String getLoans(HttpServletRequest request,HttpServletResponse response,LoanSearchCondition loanSearchCondition){
		/*返回数据对象*/
		List<Loan> loanList = null;
		/*获取页数，为空表示请求第一页数据*/
		String pgIdx  = request.getParameter("pageIndex");
		pgIdx = (pgIdx == null || pgIdx =="")?"1":pgIdx;
		Integer pageIndex = Integer.parseInt(pgIdx);
		/*组装条件对象*/
		loanSearchCondition.setOffset((pageIndex-1)*PAGE_SIZE);
		loanSearchCondition.setRowCount(PAGE_SIZE);
		/*查询数据*/
		loanList = loanService.showLoan(loanSearchCondition);
		/*预防List为空*/
		if(loanList == null  || loanList.size() <= 0 ){
			loanList = new ArrayList<Loan>();
		}
		return AjaxMsg.newSuccess().addData("datas", loanList).toJson();
	}
	
	/**
	 * 获取标的详细信息
	 * 该方法访问需要登录
	 * @param loanSearchCondition
	 * @return
	 */
	@RequestMapping(value="getLoanById")
	public @ResponseBody String getLoanById(HttpServletRequest request,HttpServletResponse response,LoanSearchCondition loanSearchCondition){
		Loan loanDetails = null;
		
		
		return "";
	}
	
	/**
	 * 申请贷款
	 * @param loanReceipt
	 * @return
	 */
	@RequestMapping(value="applyLoan")
	public @ResponseBody String applyLoan(HttpServletRequest request,HttpServletResponse response,LoanReceipt loanReceipt){
		AjaxMsg ajaxMsg = null;
		
		/*
		//  验证码验证	
	    String code = StringUtils.upperCase(request.getParameter("loanapply_imgCode"));
		String code2 = (String) request.getSession(true).getAttribute(
				CheckCodeUtil.SESSION_IMAGECODE);
		if (code.equalsIgnoreCase(code2) == false) {
			AjaxMsg msg = AjaxMsg.newError().addMessage(
					getText("user.login.captcha_error"));
			response.setCharacterEncoding("UTF-8");
			return msg.toJson();
		}
	 
	
		loanReceipt.setLr_state(Receipt.ReceiptState.STATE.toString());
		if(loanReceipt.getUser_province()==""){
			loanReceipt.setLr_user_province(null);
		}else{
			loanReceipt.setLr_user_province(loanReceipt.getUser_province());
		}
		if(loanReceipt.getUser_city()==""){
			loanReceipt.setLr_user_city(null);
		}else{
			loanReceipt.setLr_user_city(loanReceipt.getUser_city());
		}
		loanReceiptService.savaReceipt(loanReceipt);
		//申请贷款通知用户
		notifyService.shortcutFinancing(loanReceipt.getLr_user_name(), loanReceipt.getLr_user_phone(), loanReceipt.getLr_loan_amount(),Notify.APPLY_FOR_LOAN);
		*/
		ajaxMsg = AjaxMsg.newSuccess().addMessage("申请成功！");
		return ajaxMsg.toJson();
	}
	
	
	
	
	
	
}
